package com.example.order.controller;


import com.example.common.result.PageBean;
import com.example.common.result.Result;
import com.example.model.order.dto.OrdersCustonmerDTO;
import com.example.order.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @GetMapping("/admin/orders")
    public Result page(@RequestParam(value = "page", defaultValue = "1") Integer page ,
                       @RequestParam(value = "pageSize", defaultValue = "10")Integer pageSize,
                       @DateTimeFormat(pattern= "yyyy-MM-dd HH:mm:ss") LocalDateTime orderDateStart,
                       @DateTimeFormat(pattern= "yyyy-MM-dd HH:mm:ss") LocalDateTime orderDateEnd,
                       @RequestParam(value = "shipmentAddress", required = false) String shipmentAddress,
                       @RequestParam(value = "customerName", required = false) String customerName) {
        PageBean page1 = ordersService.page(page, pageSize, orderDateStart,orderDateEnd, shipmentAddress, customerName);
        return Result.success(page1);
    }

    @DeleteMapping("/admin/orders/{ids}")
    public Result delete(@PathVariable(required = true) List<Integer> ids){
        ordersService.delete(ids);
        return Result.success();
    }

    @PostMapping("/admin/orders")
    public Result save(@RequestBody OrdersCustonmerDTO ordersCustonmerDTO){
        ordersService.save(ordersCustonmerDTO);
        return Result.success();
    }
    /**
     * 更新回显
     * @param id
     * @return
     */
    @GetMapping("/admin/orders/{id}")
    public Result selectById(@PathVariable Integer id){
        OrdersCustonmerDTO ordersCustonmerDTO = ordersService.selectById(id);
        return Result.success(ordersCustonmerDTO);
    }

    @PutMapping("/admin/orders")
    public Result updateById(@RequestBody OrdersCustonmerDTO ordersCustonmerDTO){
        ordersService.updateById(ordersCustonmerDTO);
        return Result.success();
    }


}
